package Practice8_JSON;

import Utils.io.IOUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;

public class P1 {
    @Test
    public void test01() throws IOException {
        InputStream is = IOUtils.file2InputStream(new File("./src/test/resources/test.json"));
        ObjectMapper mapper = new ObjectMapper();
        // 反序列化时忽略不存在的JavaBean属性:
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Book book = mapper.readValue(is, Book.class);
        System.out.println(book);
    }

    @Test
    public void test02() {
        File file = new File("./src/test/resources/test.json");
        System.out.println(file.exists());
    }

    @Test
    public void test03() throws JsonProcessingException {
        Author author = new Author("XYZ", "WWW");
        Book book = new Book(123, "ABC", author, "123456789", new ArrayList<String>(Arrays.asList("科幻", "爱情")));
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(book);
        System.out.println(jsonStr);
    }
}
